package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.pay.models.dto.PayBankInfoDTO;
import com.ztgf.pay.models.service.IPayBankInfoService;
import com.ztgf.pay.models.vo.PayBankInfoVO;
import com.ztgf.shopping.models.bo.shop.ShopReceiveCardSaveBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.ztgf.shopping.models.service.shop.IShopReceiveCardService;
import com.ztgf.shopping.models.bo.shop.ShopReceiveCardBO;
import com.ztgf.shopping.models.dto.shop.ShopReceiveCardDTO;

import java.math.BigDecimal;

/**
 * 收款账号表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 14:33:37
 */

@RestController
@RequestMapping("/api/shopReceiveCard")
@Slf4j
@Api(value = "ShopReceiveCardApiController",tags = "收款账号表Api接口")
public class ShopReceiveCardApiController {

    @DubboReference
    private IShopReceiveCardService shopReceiveCardService;
    @DubboReference
    private IPayBankInfoService iPayBankInfoService;

    @ApiOperation(value = "添加收款账号表",notes = "添加收款账号表")
    @PostMapping(value = "/insert")
    public Result insert( ShopReceiveCardSaveBO saveBO) {
        //验证开户行地址
        PayBankInfoVO PayBankInfoVO = iPayBankInfoService.getByNo(saveBO.getAccountBankNo());
        if (PayBankInfoVO == null || !saveBO.getBankAddress().equals(PayBankInfoVO.getBankNameAddress())){
            return new Result<>(CodeEnum.FAIL_100047);
        }

        boolean flag = shopReceiveCardService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "重新绑卡",notes = "重新绑卡")
    @PostMapping(value = "/updateBindStatus")
    public Result updateBindStatus(Long id) {
        boolean flag = shopReceiveCardService.updateBindStatus(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_200006);
        }
    }

    @ApiOperation(value = "去激活卡片",notes = "去激活卡片")
    @PostMapping(value = "/goActiveCard")
    public Result goActiveCard(Long id) {
        return shopReceiveCardService.goActiveCard(id);
    }

    @ApiOperation(value = "激活卡片",notes = "激活卡片")
    @PostMapping(value = "/activeCard")
    public Result activeCard(Long id, BigDecimal money) {
        return shopReceiveCardService.activeCard(id, money);
    }


//    @PostMapping("/updateStatus")
//    @ApiOperation(value = "修改状态",notes = "修改状态")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
//            ,@ApiImplicitParam(name = "status",value = "状态 0正常、1删除",required = true,paramType = "query", dataType = "Integer")
//    })
//    public Result updateStatus(Long id, Integer status){
//        ShopReceiveCardDTO dto = new ShopReceiveCardDTO();
//        dto.setId(id);
//        dto.setStatus(status);
//        boolean flag = shopReceiveCardService.updateById(dto);
//        if(flag){
//            return new Result<>();
//        } else {
//            return new Result<>(CodeEnum.FAIL_100008);
//        }
//    }


    @ApiOperation(value = "删除收款账号(未绑定成功)",notes = "删除收款账号(未绑定成功)")
    @ApiImplicitParam(name = "id",value = "收款账号表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){

        boolean flag = shopReceiveCardService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }


    @ApiOperation(value = "注销收款账号",notes = "注销收款账号")
    @ApiImplicitParam(name = "id",value = "收款账号表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/deleteLogout")
    public Result deleteLogout(@RequestParam("id") Long id){

        boolean flag = shopReceiveCardService.deleteCard(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/updateDefault")
    @ApiOperation(value = "更新默认收款账户",notes = "更新默认收款账户")
    public Result updateDefault(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        boolean flag = shopReceiveCardService.updateDefault(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopReceiveCardDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopReceiveCardDTO dto = shopReceiveCardService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "收款账号表列表",notes = "收款账号表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopReceiveCardDTO>> queryPageList( ShopReceiveCardBO bo){
        Wrapper<ShopReceiveCardDTO> wrapper = new EntityWrapper<>();
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }
        wrapper.eq("type", 0); //只查正常状态的
        wrapper.eq("status", 0); //只查正常状态的
        wrapper.orderBy("create_time", false);//降序
        Page<ShopReceiveCardDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopReceiveCardService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
